<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>时间</title>

    <script>
        //1:实现时间的动态显示功能，要求能显示星期几，年月日，小时分钟秒
        function getTime() {
            var now = new Date();
            var year = now.getFullYear();
            var month = now.getMonth();
            var date = now.getDate();
            var hour = now.getHours();
            var day = "AM";
            if (now.getHours() > 12) {
                hour -= 12;
                day = "PM"
            }
            var min = now.getMinutes();
            var sec = now.getSeconds();
            var week = ["一", "二", "三", "四", "五", "六", "日"];
            var weekNum = now.getDay();
            document.getElementById("time").innerHTML = `现在是${year}年${month + 1}月${date}日 ${hour}时${min}分${sec}秒 ${day} 星期${week[weekNum - 1]}`;
        }

        var t;

        function showTime() {
            t = setInterval(getTime, 1000);

        }

        function stopTime(obj) {
            clearInterval(obj)
        }

        //2:时间倒计时功能，倒计时10秒后，跳转到百度。页面要动态显示剩余时间
        var t2;
        function clock() {
            if (document.getElementById("time2").innerText == 0) {
                //location.href = "https://wwww.baidu.com/";
                open("https://wwww.baidu.com/")
                stopTime(t2);
                return;
            }
           document.getElementById("time2").innerText -= 1;
        }

        function clockRun(startTime) {
            document.getElementById("time2").innerText =startTime;
            t2 =  setInterval(clock, 1000);
        }


    </script>
</head>
<body>
<input type="button" value="显示时间" name="time" onclick="showTime()">
<input type="button" value="时间停止" name="time" onclick="stopime(t)">
<input type="button" value="倒计时十秒" name="time" onclick="clockRun(10)">
<input type="button" value="倒计时结束" name="time" onclick="stopTime(t2)">
<div id="time"></div>
<div id="time2"></div>
</body>
</html>